<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>SpringBoot之JPA数据查询备忘 - Lglspace‘s Blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="lglspace" /><meta name="description" content="查询类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package cn.lglspace.stulab.repository; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import cn.lglspace.stulab.domain.Teacher; @Repository" /><meta name="keywords" content="lglspace, blog, lgl" />






<meta name="generator" content="Hugo 0.79.0 with theme even" />


<link rel="canonical" href="https://lglspace.cn/post/java/java-jpa-query/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/even-custom.css">


<meta property="og:title" content="SpringBoot之JPA数据查询备忘" />
<meta property="og:description" content="查询类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package cn.lglspace.stulab.repository; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import cn.lglspace.stulab.domain.Teacher; @Repository" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://lglspace.cn/post/java/java-jpa-query/" />
<meta property="article:published_time" content="2020-11-06T22:00:45+08:00" />
<meta property="article:modified_time" content="2020-11-06T22:00:45+08:00" />
<meta itemprop="name" content="SpringBoot之JPA数据查询备忘">
<meta itemprop="description" content="查询类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package cn.lglspace.stulab.repository; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import cn.lglspace.stulab.domain.Teacher; @Repository">
<meta itemprop="datePublished" content="2020-11-06T22:00:45+08:00" />
<meta itemprop="dateModified" content="2020-11-06T22:00:45+08:00" />
<meta itemprop="wordCount" content="603">



<meta itemprop="keywords" content="java,JPA,springboot," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="SpringBoot之JPA数据查询备忘"/>
<meta name="twitter:description" content="查询类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package cn.lglspace.stulab.repository; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import cn.lglspace.stulab.domain.Teacher; @Repository"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Lglspace</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Lglspace</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">SpringBoot之JPA数据查询备忘</h1>

      <div class="post-meta">
        <span class="post-time"> 2020-11-06 </span>
        <div class="post-category">
            <a href="/categories/java/"> java </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li>
          <ul>
            <li><a href="#查询类">查询类</a></li>
            <li><a href="#测试方法">测试方法</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h3 id="查询类">查询类</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">cn.lglspace.stulab.repository</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.springframework.data.domain.Page</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.data.domain.Pageable</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.data.jpa.domain.Specification</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.data.jpa.repository.JpaRepository</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.data.jpa.repository.Query</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.data.repository.query.Param</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Repository</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">cn.lglspace.stulab.domain.Teacher</span><span class="o">;</span>

<span class="nd">@Repository</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">TeacherRepository</span> <span class="kd">extends</span> <span class="n">JpaRepository</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">,</span> <span class="n">Long</span><span class="o">&gt;</span> <span class="o">{</span>

	<span class="c1">// 按照方法名进行查询
</span><span class="c1"></span>	<span class="n">List</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="nf">findByName</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">);</span>

	<span class="n">Teacher</span> <span class="nf">findFirstByOrderByNameAsc</span><span class="o">();</span>

	<span class="n">Teacher</span> <span class="nf">findTopByOrderByNameDesc</span><span class="o">();</span>

	<span class="c1">// 计数
</span><span class="c1"></span>	<span class="kt">long</span> <span class="nf">countByName</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">);</span>

	<span class="c1">// SQL查询
</span><span class="c1"></span>	<span class="nd">@Query</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;select * from teacher where name like ?1 order by id&#34;</span><span class="o">,</span> 
			<span class="n">countQuery</span> <span class="o">=</span> <span class="s">&#34;select count(*) from teacher where name like ?1&#34;</span><span class="o">,</span> <span class="n">nativeQuery</span> <span class="o">=</span> <span class="kc">true</span><span class="o">)</span>
	<span class="n">Page</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="nf">findByNameLikeWithSql</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="n">Pageable</span> <span class="n">pageable</span><span class="o">);</span>

	<span class="c1">// HQL查选
</span><span class="c1"></span>	<span class="nd">@Query</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;select t.name from Teacher t where t.id = ?1&#34;</span><span class="o">)</span>
	<span class="n">String</span> <span class="nf">findNameById</span><span class="o">(</span><span class="n">Long</span> <span class="n">id</span><span class="o">);</span>

	<span class="c1">// 分页查询
</span><span class="c1"></span>	<span class="n">Page</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="nf">findAllByIdIn</span><span class="o">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">Long</span><span class="o">&gt;</span> <span class="n">teacherIds</span><span class="o">,</span> <span class="n">Pageable</span> <span class="n">pageable</span><span class="o">);</span>

	<span class="nd">@Query</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;select t.name from Teacher t where t.id in :ids&#34;</span><span class="o">)</span>
	<span class="n">Page</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="nf">findNamesByIds</span><span class="o">(</span><span class="nd">@Param</span><span class="o">(</span><span class="s">&#34;ids&#34;</span><span class="o">)</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">Long</span><span class="o">&gt;</span> <span class="n">ids</span><span class="o">,</span> <span class="n">Pageable</span> <span class="n">pageable</span><span class="o">);</span>

	<span class="n">Page</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="nf">findAll</span><span class="o">(</span><span class="n">Specification</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="n">spec</span><span class="o">,</span> <span class="n">Pageable</span> <span class="n">pageable</span><span class="o">);</span>

<span class="o">}</span>

</code></pre></td></tr></table>
</div>
</div><h3 id="测试方法">测试方法</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="nd">@Test</span>
<span class="kt">void</span> <span class="nf">testQuery</span><span class="o">()</span> <span class="o">{</span>
	
	<span class="n">List</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="n">allTeachers</span> <span class="o">=</span> <span class="n">teacherRepository</span><span class="o">.</span><span class="na">findAll</span><span class="o">();</span>
	
	<span class="kd">final</span> <span class="n">String</span> <span class="n">judgeName</span> <span class="o">=</span> <span class="s">&#34;于老师1&#34;</span><span class="o">;</span>
	<span class="kt">long</span> <span class="n">targetSize</span> <span class="o">=</span> <span class="n">allTeachers</span><span class="o">.</span><span class="na">stream</span><span class="o">().</span><span class="na">filter</span><span class="o">(</span><span class="n">item</span><span class="o">-&gt;</span><span class="n">item</span><span class="o">.</span><span class="na">getName</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">judgeName</span><span class="o">)).</span><span class="na">count</span><span class="o">();</span>
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherRepository</span><span class="o">.</span><span class="na">findByName</span><span class="o">(</span><span class="n">judgeName</span><span class="o">)).</span><span class="na">hasSize</span><span class="o">((</span><span class="kt">int</span><span class="o">)</span><span class="n">targetSize</span><span class="o">);</span>
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherRepository</span><span class="o">.</span><span class="na">countByName</span><span class="o">(</span><span class="n">judgeName</span><span class="o">)).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">targetSize</span><span class="o">);</span>
	
	<span class="n">Pageable</span> <span class="n">pageable</span> <span class="o">=</span> <span class="n">PageRequest</span><span class="o">.</span><span class="na">of</span><span class="o">(</span><span class="n">0</span><span class="o">,</span><span class="n">10</span><span class="o">);</span>
	<span class="n">Page</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="n">teacherPage</span> <span class="o">=</span> <span class="n">teacherRepository</span><span class="o">.</span><span class="na">findByNameLikeWithSql</span><span class="o">(</span><span class="n">judgeName</span><span class="o">,</span> <span class="n">pageable</span><span class="o">);</span>
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherPage</span><span class="o">.</span><span class="na">getTotalElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">targetSize</span><span class="o">);</span>
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherPage</span><span class="o">.</span><span class="na">get</span><span class="o">().</span><span class="na">mapToLong</span><span class="o">(</span><span class="n">Teacher</span><span class="o">::</span><span class="n">getId</span><span class="o">).</span><span class="na">sorted</span><span class="o">().</span><span class="na">findFirst</span><span class="o">().</span><span class="na">getAsLong</span><span class="o">())</span>
	<span class="o">.</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">allTeachers</span><span class="o">.</span><span class="na">stream</span><span class="o">().</span><span class="na">filter</span><span class="o">(</span><span class="n">item</span><span class="o">-&gt;</span><span class="n">item</span><span class="o">.</span><span class="na">getName</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">judgeName</span><span class="o">))</span>
			<span class="o">.</span><span class="na">mapToLong</span><span class="o">(</span><span class="n">Teacher</span><span class="o">::</span><span class="n">getId</span><span class="o">).</span><span class="na">sorted</span><span class="o">().</span><span class="na">findFirst</span><span class="o">().</span><span class="na">getAsLong</span><span class="o">());</span>
	
	
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherRepository</span><span class="o">.</span><span class="na">findFirstByOrderByNameAsc</span><span class="o">().</span><span class="na">getName</span><span class="o">())</span>
	<span class="o">.</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">allTeachers</span><span class="o">.</span><span class="na">stream</span><span class="o">().</span><span class="na">map</span><span class="o">(</span><span class="n">Teacher</span><span class="o">::</span><span class="n">getName</span><span class="o">).</span><span class="na">sorted</span><span class="o">().</span><span class="na">findFirst</span><span class="o">().</span><span class="na">get</span><span class="o">());</span>

	
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherRepository</span><span class="o">.</span><span class="na">findTopByOrderByNameDesc</span><span class="o">().</span><span class="na">getName</span><span class="o">())</span>
	<span class="o">.</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">allTeachers</span><span class="o">.</span><span class="na">stream</span><span class="o">().</span><span class="na">map</span><span class="o">(</span><span class="n">Teacher</span><span class="o">::</span><span class="n">getName</span><span class="o">).</span><span class="na">sorted</span><span class="o">(</span><span class="n">Comparator</span><span class="o">.</span><span class="na">reverseOrder</span><span class="o">()).</span><span class="na">findFirst</span><span class="o">().</span><span class="na">get</span><span class="o">());</span>

	<span class="n">List</span><span class="o">&lt;</span><span class="n">Long</span><span class="o">&gt;</span> <span class="n">idList</span> <span class="o">=</span> <span class="n">allTeachers</span><span class="o">.</span><span class="na">stream</span><span class="o">().</span><span class="na">filter</span><span class="o">(</span><span class="n">item</span><span class="o">-&gt;</span><span class="n">item</span><span class="o">.</span><span class="na">getName</span><span class="o">().</span><span class="na">contains</span><span class="o">(</span><span class="s">&#34;于老师&#34;</span><span class="o">))</span>
	<span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">Teacher</span><span class="o">::</span><span class="n">getId</span><span class="o">).</span><span class="na">collect</span><span class="o">(</span><span class="n">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span>
	
	<span class="n">teacherPage</span> <span class="o">=</span> <span class="n">teacherRepository</span><span class="o">.</span><span class="na">findAllByIdIn</span><span class="o">(</span><span class="n">idList</span><span class="o">,</span> <span class="n">pageable</span><span class="o">);</span>
	<span class="k">if</span><span class="o">(</span><span class="n">idList</span><span class="o">.</span><span class="na">size</span><span class="o">()&gt;=</span><span class="n">10</span><span class="o">)</span> <span class="o">{</span>
		<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherPage</span><span class="o">.</span><span class="na">getNumberOfElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">pageable</span><span class="o">.</span><span class="na">getPageSize</span><span class="o">());</span>
	<span class="o">}</span><span class="k">else</span> <span class="o">{</span>
		<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherPage</span><span class="o">.</span><span class="na">getNumberOfElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">idList</span><span class="o">.</span><span class="na">size</span><span class="o">());</span>
	<span class="o">}</span>
	
	<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherRepository</span><span class="o">.</span><span class="na">findNameById</span><span class="o">(</span><span class="n">allTeachers</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">0</span><span class="o">).</span><span class="na">getId</span><span class="o">())).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">allTeachers</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">0</span><span class="o">).</span><span class="na">getName</span><span class="o">());</span>
	
	
	<span class="n">Page</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">names</span> <span class="o">=</span> <span class="n">teacherRepository</span><span class="o">.</span><span class="na">findNamesByIds</span><span class="o">(</span><span class="n">idList</span><span class="o">,</span> <span class="n">pageable</span><span class="o">);</span>
	<span class="k">if</span><span class="o">(</span><span class="n">idList</span><span class="o">.</span><span class="na">size</span><span class="o">()&gt;=</span><span class="n">10</span><span class="o">)</span> <span class="o">{</span>
		<span class="n">assertThat</span><span class="o">(</span><span class="n">names</span><span class="o">.</span><span class="na">getNumberOfElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">pageable</span><span class="o">.</span><span class="na">getPageSize</span><span class="o">());</span>
	<span class="o">}</span><span class="k">else</span> <span class="o">{</span>
		<span class="n">assertThat</span><span class="o">(</span><span class="n">names</span><span class="o">.</span><span class="na">getNumberOfElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">idList</span><span class="o">.</span><span class="na">size</span><span class="o">());</span>
	<span class="o">}</span>
	
	<span class="c1">//规格定义
</span><span class="c1"></span>    <span class="n">Specification</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="n">specification</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Specification</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;()</span> <span class="o">{</span>
        <span class="nd">@Override</span>
        <span class="kd">public</span> <span class="n">Predicate</span> <span class="nf">toPredicate</span><span class="o">(</span><span class="n">Root</span><span class="o">&lt;</span><span class="n">Teacher</span><span class="o">&gt;</span> <span class="n">root</span><span class="o">,</span> <span class="n">CriteriaQuery</span><span class="o">&lt;?&gt;</span> <span class="n">query</span><span class="o">,</span> <span class="n">CriteriaBuilder</span> <span class="n">cb</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">List</span><span class="o">&lt;</span><span class="n">Predicate</span><span class="o">&gt;</span> <span class="n">predicates</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span> <span class="c1">//所有的断言
</span><span class="c1"></span>            
            <span class="n">Predicate</span> <span class="n">likeName</span> <span class="o">=</span> <span class="n">cb</span><span class="o">.</span><span class="na">like</span><span class="o">(</span><span class="n">root</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">&#34;name&#34;</span><span class="o">).</span><span class="na">as</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">class</span><span class="o">),</span><span class="s">&#34;于老师%&#34;</span><span class="o">);</span>
            <span class="n">predicates</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">likeName</span><span class="o">);</span>
            
            <span class="n">Predicate</span> <span class="n">eqGender</span> <span class="o">=</span> <span class="n">cb</span><span class="o">.</span><span class="na">equal</span><span class="o">(</span><span class="n">root</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">&#34;gender&#34;</span><span class="o">).</span><span class="na">as</span><span class="o">(</span><span class="n">Integer</span><span class="o">.</span><span class="na">class</span><span class="o">),</span><span class="n">1</span><span class="o">);</span>
            <span class="n">predicates</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">eqGender</span><span class="o">);</span>
            
            <span class="n">Predicate</span> <span class="n">gtSalary</span> <span class="o">=</span> <span class="n">cb</span><span class="o">.</span><span class="na">greaterThan</span><span class="o">(</span><span class="n">root</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">&#34;salary&#34;</span><span class="o">).</span><span class="na">as</span><span class="o">(</span><span class="n">BigDecimal</span><span class="o">.</span><span class="na">class</span><span class="o">),</span><span class="n">BigDecimal</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">3635</span><span class="o">.</span><span class="na">66</span><span class="o">));</span>
            <span class="n">predicates</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">gtSalary</span><span class="o">);</span>
            
            <span class="k">return</span> <span class="n">cb</span><span class="o">.</span><span class="na">and</span><span class="o">(</span><span class="n">predicates</span><span class="o">.</span><span class="na">toArray</span><span class="o">(</span><span class="k">new</span> <span class="n">Predicate</span><span class="o">[</span><span class="n">0</span><span class="o">]));</span>
        <span class="o">}</span>
    <span class="o">};</span>
    
    <span class="n">idList</span> <span class="o">=</span> <span class="n">allTeachers</span><span class="o">.</span><span class="na">stream</span><span class="o">().</span><span class="na">filter</span><span class="o">(</span><span class="n">item</span><span class="o">-&gt;</span>
    <span class="n">item</span><span class="o">.</span><span class="na">getName</span><span class="o">().</span><span class="na">startsWith</span><span class="o">(</span><span class="s">&#34;于老师&#34;</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="n">item</span><span class="o">.</span><span class="na">getGender</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">1</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="n">item</span><span class="o">.</span><span class="na">getSalary</span><span class="o">().</span><span class="na">compareTo</span><span class="o">(</span><span class="n">BigDecimal</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">3635</span><span class="o">.</span><span class="na">66</span><span class="o">))&gt;</span><span class="n">0</span><span class="o">)</span>
    		<span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">Teacher</span><span class="o">::</span><span class="n">getId</span><span class="o">).</span><span class="na">collect</span><span class="o">(</span><span class="n">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span>
   
	<span class="n">teacherPage</span> <span class="o">=</span> <span class="n">teacherRepository</span><span class="o">.</span><span class="na">findAll</span><span class="o">(</span><span class="n">specification</span><span class="o">,</span> <span class="n">pageable</span><span class="o">);</span>
	<span class="k">if</span><span class="o">(</span><span class="n">idList</span><span class="o">.</span><span class="na">size</span><span class="o">()&gt;=</span><span class="n">10</span><span class="o">)</span> <span class="o">{</span>
		<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherPage</span><span class="o">.</span><span class="na">getNumberOfElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">pageable</span><span class="o">.</span><span class="na">getPageSize</span><span class="o">());</span>
	<span class="o">}</span><span class="k">else</span> <span class="o">{</span>
		<span class="n">assertThat</span><span class="o">(</span><span class="n">teacherPage</span><span class="o">.</span><span class="na">getNumberOfElements</span><span class="o">()).</span><span class="na">isEqualTo</span><span class="o">(</span><span class="n">idList</span><span class="o">.</span><span class="na">size</span><span class="o">());</span>
	<span class="o">}</span>
	
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">lglspace</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2020-11-06
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/java/">java</a>
          <a href="/tags/jpa/">JPA</a>
          <a href="/tags/springboot/">springboot</a>
          </div>
      <nav class="post-nav">
        
        <a class="next" href="/post/windows/windows-csv-errorcode/">
            <span class="next-text nav-default">CSV文件用Excel打开中文乱码</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:lglspace@outlook.com" class="iconfont icon-email" title="email"></a>
  <a href="https://lglspace.cn/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  

  

  <span class="copyright-year">
    &copy; 
    2020 - 
    2021<span class="heart"><i class="iconfont icon-heart"></i></span><span>lglspace</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.a74e82b84b872f6b028df4e965781ddbba1606f0253b4fad5e4cf3913cf576eb.js"></script>






<script src="/js/site-custom.js"></script>


</body>
</html>
